import { apiAddAccount, apiUpdateAccount } from '../../api/index'
import { getRoute } from '../../common/js/mixin'

let page = {
  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    phone: '',
    account: '',
    status: '微信',
    show: false,
    textNUm: 2
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    if (options.data) {
      const data = JSON.parse(options.data)
      this.setData({
        name: data.realname,
        phone: data.mobile,
        account: data.account_number,
        status: data.account_type_name,
        textNUm: data.account_type,
        id: data.id
      })
    }
  },
  showPop () {
    this.setData({
      show: true
    })
  },
  onClose () {
    this.setData({
      show: false
    })
  },
  // 微信
  selectWx (e) {
    this.setData({
      status: e.currentTarget.dataset.item,
      show: false,
      textNUm: 2
    })
  },
  // 支付宝
  selectQ (e) {
    this.setData({
      status: e.currentTarget.dataset.item,
      show: false,
      textNUm: 3
    })
  },
  cancelPop () {
    this.setData({
      show: false
    })
  },
  // 监听input
  inputBlur (e) {
    let index = e.currentTarget.dataset.index
    let value = e.detail.value
    switch (index) {
      case '1':
        this.setData({
          name: value
        })
        break
      case '2':
        this.setData({
          phone: value
        })
        break
      case '4':
        this.setData({
          account: value
        })
        break
    }
  },
  // 新增
  addNew () {
    if (this.data.name) {
      if (/^[1][3,4,5,7,8][0-9]{9}$/.test(this.data.phone)) {
        if (this.data.account) {
          if (this.data.id) {
            // 编辑
            apiUpdateAccount({
              real_name: this.data.name,
              mobile: this.data.phone,
              account_type: this.data.textNUm,
              account_type_name: this.data.status,
              account_number: this.data.account,
              id: this.data.id
            }).then(res => {
              wx.showToast({
                title: '编辑成功'
              })
              const route = this.getRoute()
              route.getAccountList()
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1
                })
              }, 1000)
            })
          } else {
            // 新增
            apiAddAccount({
              real_name: this.data.name,
              mobile: this.data.phone,
              account_type: this.data.textNUm,
              account_type_name: this.data.status,
              account_number: this.data.account
            }).then(
              res => {
                wx.showToast({
                  title: '新增成功'
                })
                const route = this.getRoute()
                route.getAccountList()
                setTimeout(() => {
                  wx.navigateBack({
                    delta: 1
                  })
                }, 1000)
              }
            )
          }
        } else {
          wx.showToast({
            title: '请填写正确账号',
            icon: 'none'
          })
        }
      } else {
        wx.showToast({
          title: '请填写正确的电话号码',
          icon: 'none'
        })
      }
    } else {
      wx.showToast({
        title: '请填写真实姓名',
        icon: 'none'
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {

  }
}
getRoute(page)
Page(page)
